package chen.you.comm.log

import android.util.Log
import androidx.annotation.IntDef
import com.lz.library.extension.stackString

/**
 * author: you : 2018/12/27
 */
object LogUtils {

    const val VERBOSE = Log.VERBOSE

    const val DEBUG = Log.DEBUG

    const val INFO = Log.INFO

    const val WARN = Log.WARN

    const val ERROR = Log.ERROR

    @IntDef(VERBOSE, DEBUG, INFO, WARN, ERROR)
    @Retention(AnnotationRetention.SOURCE)
    annotation class Level

    private var logger: Logger? = null

    fun initLogger(logger: Logger) {
        LogUtils.logger = logger
    }

    fun flush() {
        logger?.flush()
    }

    //一般不使用
    fun release() {
        logger?.release()
        logger = null
    }

    fun leveChar(@Level level: Int): Char = when (level) {
        VERBOSE -> 'V'
        DEBUG -> 'D'
        INFO -> 'I'
        WARN -> 'W'
        ERROR -> 'E'
        else -> ' '
    }

    fun v(tag: String, msg: String) {
        logger?.println(VERBOSE, tag, msg)
    }

    fun d(tag: String, msg: String) {
        logger?.println(DEBUG, tag, msg)
    }

    fun i(tag: String, msg: String) {
        logger?.println(INFO, tag, msg)
    }

    fun w(tag: String, msg: String) {
        logger?.println(WARN, tag, msg)
    }

    fun w(tag: String, t: Throwable) {
        w(tag, t.stackString())
    }

    fun e(tag: String, msg: String) {
        logger?.println(ERROR, tag, msg)
    }

    fun e(tag: String, t: Throwable) {
        e(tag, t.stackString())
    }

}